﻿#include "OperSubsystemFE.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "FITK_Component/FITKAcousticsModel/FITKFESubSysStructure.h"
#include "FITK_Component/FITKAcousticsModel/FITKFESubSysAcoustic.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include "updateBrowserTable.h"
#include "GUIWidget/AttributeSetDialog.h"

#include <QMessageBox>

namespace ModelOper
{
    bool OperSubsystemFE::execGUI()
    {
        return false;
    }
    bool OperSubsystemFE::execProfession()
    {
        return false;
    }

    void OperSubsystemFE::editSubsystem(const int feID)
    {
        if (!_seaMgr)return;
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //编辑对话框
        QObject object;
        object.setObjectName("actionEditFESubsystem");
        Core::FITKActionOperator* oper = Core::FITKOperatorRepo::getInstance()->getOperatorT<Core::FITKActionOperator>("actionEditFESubsystem");
        if (!oper) return;
        oper->setArgs("DataID", feID);
        oper->setEmitter(&object);
        oper->actionTriggered();
    }

    void OperSubsystemFE::removeSubsystem(const int feID)
    {
        //获取管理器
        if (!_feMgr)return;
        //移除并更新列表
        _feMgr->removeDataByID(feID);
        ModelOper::updateBrowserTable();
        EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        operGraph->updateFESubSystems();
    }

    void OperSubsystemFE::setAttribute(const QString & name, const QString & text, const int attrIndex, const QList<int>& feIDs)
    {
        //获取管理器
        if (!_feMgr)return;
        Acoustics::FITKAbstractFESubSys* fe = dynamic_cast<Acoustics::FITKAbstractFESubSys*>(_feMgr->getDataByID(feIDs[0]));
        if (!fe)return;
        //根据拾取列表的第一个元素ID确定fe类型
        Acoustics::FITKAbstractFESubSys::FESubSysType type = fe->getFESubSysType();
        if (type== Acoustics::FITKAbstractFESubSys::FESubSysType::FEStructure) {
            setAttributeFEStructure(name, text, attrIndex, feIDs);
        }
        else if (type == Acoustics::FITKAbstractFESubSys::FESubSysType::FEAcoustic) {
            setAttributeFEAcoustic(name, text, attrIndex, feIDs);
        }
        //刷新表格
        ModelOper::updateBrowserTable();
    }

    void OperSubsystemFE::setAttributeFEStructure(const QString & name, const QString & text, const int attrIndex, const QList<int>& feIDs)
    {
        if (!_feMgr || !_specMgr)return;
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //设置对话框
        GUI::AttributeSetDialog dlg(2, mw);
        dlg.setTextLabel(name);
        //填充下拉列表
        QList<QString> list;
        QList<Acoustics::FITKAcousticsAbstractSpectra*> specList = _specMgr->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::Damping);
        for (auto value : specList)
            list += value->getDataObjectName();
        dlg.setDefaultValueQString(list);

        if (dlg.exec() != QDialog::Accepted) return;

        QString specName = dlg.getValueQString();
        int specID = -1;
        if (_specMgr->getDataByName(specName)) {
            specID = _specMgr->getDataByName(specName)->getDataObjectID();
        }

        for (int id : feIDs)
        {
            Acoustics::FITKFESubSysStructure* fe = dynamic_cast
                <Acoustics::FITKFESubSysStructure*>(_feMgr->getDataByID(id));
            if (fe == nullptr) continue;
            //只有频谱能批量
            fe->setDampingLossSpectraID(specID);
        }
    }

    void OperSubsystemFE::setAttributeFEAcoustic(const QString & name, const QString & text, const int attrIndex, const QList<int>& feIDs)
    {
        if (!_feMgr || !_specMgr)return;
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //获取材料管理器
        Acoustics::FITKAcousticsPhysics* phyMgr = FITKAPP->getGlobalData()->getPhysicsData<Acoustics::FITKAcousticsPhysics>();
        if (!phyMgr)return;
        Acoustics::FITKAcousticsMaterialManager* materialMgr = phyMgr->getMaterialManager();
        if (!materialMgr)return;
        //设置对话框
        GUI::AttributeSetDialog dlg(2, mw);
        dlg.setTextLabel(name);
        //填充下拉列表
        QList<QString> list;
        if (attrIndex==2) {
            QList<Acoustics::FITKAcousticsAbstractSpectra*> specList = _specMgr->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::Damping);
            specList+= _specMgr->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::Absorption);
            for (auto value : specList)
                list += value->getDataObjectName();
        }
        else if (attrIndex == 1) {
            QList<Acoustics::FITKAcousticsAbsMaterial*> materialList = materialMgr->getAcousticsMaterials(Acoustics::FITKAcousticsAbsMaterial::AcousticsMaterialType::Fluid);
            for (auto value : materialList)
                list += value->getDataObjectName();
        }
        dlg.setDefaultValueQString(list);

        if (dlg.exec() != QDialog::Accepted) return;

        QString str = dlg.getValueQString();
        int ID = -1;
        if (attrIndex == 2) {
            if (_specMgr->getDataByName(str))
                ID = _specMgr->getDataByName(str)->getDataObjectID();
        }
        if (attrIndex == 1) {
            if (materialMgr->getDataByName(str))
                ID = materialMgr->getDataByName(str)->getDataObjectID();
        }
        for (int id : feIDs)
        {
            Acoustics::FITKFESubSysAcoustic* fe = dynamic_cast
                <Acoustics::FITKFESubSysAcoustic*>(_feMgr->getDataByID(id));
            if (fe == nullptr) continue;
            switch (attrIndex)
            {
            case 1:
                fe->setAllElementPropID(ID);
                break;
            case 2:
                fe->setDampingSpectraID(ID);
                break;
            default:
                break;
            }    
        }
    }
}